

package com.zdsx.modules.production.controller;

import com.zdsx.common.exception.CommonException;
import com.zdsx.common.exception.ErrorCode;
import com.zdsx.common.page.PageData;
import com.zdsx.common.utils.Result;
import com.zdsx.modules.oss.entity.SysOssEntity;
import com.zdsx.modules.production.dto.CategoryDTO;
import com.zdsx.modules.production.dto.ProductInfoDTO;
import com.zdsx.modules.production.entity.ProductInfo;
import com.zdsx.modules.production.service.ProductService;
import com.zdsx.modules.security.user.SecurityUser;
import com.zdsx.modules.security.user.UserDetail;
import com.zdsx.modules.sys.dto.SysUserDTO;
import com.zdsx.modules.sys.service.SysDictDataService;
import com.zdsx.modules.sys.service.SysDictTypeService;
import com.zdsx.modules.sys.service.SysRoleDataScopeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 用户产品
 *
 * @author Lr
 * @since 1.0.0
 */
@RestController
@RequestMapping("alarm/product")
@Api(tags = "中电赛西产品")
@AllArgsConstructor
public class ProductController {

    private final ProductService productService;

    @ApiOperation(value = "获取产品分类")
    @GetMapping("category/list")
//    @RequiresPermissions("product:category:list")
    public Result<List<CategoryDTO>> categoryList() {
        List<CategoryDTO> categoryList = productService.getProductCategoryList();
        return new Result<List<CategoryDTO>>().ok(categoryList);
    }

    /**
     * 添加产品
     * @return null
     */
    @ApiOperation(value = "添加产品")
    @PostMapping//("add")
//    @RequiresPermissions("product:add")
    public Result<Object> productAdd(@Valid @RequestBody ProductInfoDTO productInfoDTO) {
        productService.insert(productInfoDTO);
        return new Result<>();
    }


    /**
     * 更新产品
     * @param productInfoDTO
     * @return null
     */
    @PutMapping//("update")
    @ApiOperation(value = "更新产品")
//    @RequiresPermissions("product:update")
    public Result<Object> update(@Valid @RequestBody ProductInfoDTO productInfoDTO) {
        if (productInfoDTO.getId() == null) {
            throw new CommonException(ErrorCode.PARAMETER_ERROR, "id不能为空");
        }
        boolean result = productService.update(productInfoDTO);
        if (!result) {
            throw new CommonException(ErrorCode.UPDATE_FAILED, "更新失败，请查询数据是否正确");
        }
        return new Result<>();
    }


    /**
     * 删除产品
     * @param ids
     * @return null
     */
    @ApiOperation(value = "删除产品")
    @DeleteMapping
//    @RequiresPermissions("product:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new CommonException(ErrorCode.PARAMETER_ERROR, "id不能为空");
        }
        boolean result = productService.delete(ids);
        if (!result) {
            throw new CommonException(ErrorCode.UPDATE_FAILED, "删除失败，请查询数据是否正确");
        }
        return new Result<>();
    }


    @GetMapping("{id}")
    @ApiOperation("信息")
//    @RequiresPermissions("sys:user:info")
    public Result<ProductInfoDTO> get(@PathVariable("id") Long id) {
        //用户角色列表
        ProductInfoDTO dto = productService.getById(id);
        return new Result<ProductInfoDTO>().ok(dto);
    }

    /**
     * 查询产品列表： 查全部的产品（在编辑角色的时候使用）
     * @return null
     */
//    @RequiresPermissions("product:list")
    @ApiOperation(value = "查询全部产品")
    @GetMapping("list")
    public Result<List<ProductInfoDTO>> list() {
        List<ProductInfoDTO> productInfoDTOS = productService.productList();
        return new Result<List<ProductInfoDTO>>().ok(productInfoDTOS);
    }


    /**
     * 查询产品列表： 分页查询
     * 先去掉权限校验
     * @return null
     */
//    @RequiresPermissions("product:page")
    @GetMapping("page")
    @ApiOperation(value = "分页查询产品列表")
    public Result<PageData<ProductInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<ProductInfoDTO> page = productService.page(params);
        return new Result<PageData<ProductInfoDTO>>().ok(page);
    }


}